//
//  WXPAlreadyAuthHomeVC.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/7.
//

#import "WXPAlreadyAuthHomeVC.h"
#import "WXPAlreadyAuthLimitView.h"
#import "WXPAlreadyAuthGoLoanView.h"
#import "WXPHomeCloundReportView.h"
#import "WXPAlreadyAuthAmendBankCardView.h"
#import "WXPAlreadyAuthGoPayBackView.h"
#import "WXPFicoReport.h"



@interface WXPAlreadyAuthHomeVC ()

@property (nonatomic, strong) WXPNavView  * navView;

@property (nonatomic, strong) UIView * headerView;
@property (nonatomic, strong) WXPAlreadyAuthLimitView * limitView;
@property (nonatomic, strong) WXPAlreadyAuthGoLoanView * goLoanView;
@property (nonatomic, strong) WXPTwoBtnPopUpWindowView * noProductWindowView;
@property (nonatomic, strong) WXPTwoBtnPopUpWindowView * toCpiListWindowView;
@property (nonatomic, strong) WXPHomeCloundReportView * cloundReportView;
@property (nonatomic, strong) WXPFicoReport * ficoReport;

@property (nonatomic, strong) WXPAlreadyAuthAmendBankCardView * amendBankCardView;
@property (nonatomic, assign) BOOL isShowAmendBankCardView;
@property (nonatomic, strong) WXPAlreadyAuthGoPayBackView * goPayBackView;
@property (nonatomic, assign) BOOL isShowGoPayBackView;





@end

@implementation WXPAlreadyAuthHomeVC

#pragma mark - lazy
- (WXPNavView *)navView{
    if (!_navView) {
        _navView = [WXPNavView new];
        _navView.leftTop = XY(0, 0);
        _navView.backBtn.hidden = true;
        [_navView resetViewWithTitle:[GlobalMethod getAppName]];
        [_navView addSubview:self.btnNotification];
    }
    return _navView;
}
- (UIButton *)btnNotification
{
    if (_btnNotification == nil) {
        _btnNotification = [UIButton  new];
        _btnNotification.widthHeight = XY(32, 44);
        _btnNotification.rightBottom = XY(self.navView.btnService.left-LEFT_MARDIN, self.navView.height);
        [_btnNotification setImage:[UIImage imageNamed:@"icon-home-notification"] forState:UIControlStateNormal];
        _btnNotification.hidden = true;
        WEAKSELF
        [_btnNotification setNoRepeatActionWithInterval:1 action:^(UIButton * _Nonnull button) {
            [NSObject clickFeedback];
            [weakSelf.btnNotification addClickAnimate];
            if(weakSelf.blockGetNotificationView){
                weakSelf.blockGetNotificationView();
            }
        }];
    }
    return  _btnNotification;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [UIView new];
        _headerView.backgroundColor = [UIColor clearColor];
    }
    return _headerView;
}
// 顶部用户额度
- (WXPAlreadyAuthLimitView *)limitView{
    if (!_limitView) {
        _limitView = [WXPAlreadyAuthLimitView new];
    }
    return _limitView;
}
// 去借款
- (WXPAlreadyAuthGoLoanView *)goLoanView{
    if (!_goLoanView) {
        _goLoanView = [WXPAlreadyAuthGoLoanView new];
        WEAKSELF
        _goLoanView.blockGoLoan = ^{
            [GlobalMethod hitPointWithPageName:KEY_Home action:KEY_Click extInfo:nil];
            [weakSelf requestIsJumpToCPI];
        };
    }
    return _goLoanView;
}
// 无产品弹窗
- (WXPTwoBtnPopUpWindowView *)noProductWindowView{
    if (!_noProductWindowView) {
        _noProductWindowView = [WXPTwoBtnPopUpWindowView new];
        ModelBtn * model = [ModelBtn new];
        model.content = @"En este momento, no hay un producto adecuado para usted. Por favor, regrese más tarde.";
        model.rightName = @"OK";
        model.isHide = true;
        [_noProductWindowView resetWithModel:model];
        [_noProductWindowView resetOnlyShowRightBtn];
    }
    return _noProductWindowView;
}
// 无额度 cpi 列表页面
- (WXPTwoBtnPopUpWindowView *)toCpiListWindowView
{
    if (_toCpiListWindowView == nil) {
        _toCpiListWindowView = [WXPTwoBtnPopUpWindowView  new];
        ModelBtn * model = [ModelBtn new];
        model.content = @"Lamentamos que no cumpla con nuestros requisitos para productos de préstamos en este momento. Le sugerimos otros productos.";
        model.isHide = true;
        [_toCpiListWindowView resetWithModel:model];
        WEAKSELF
        _toCpiListWindowView.blockRightClick = ^{
            [weakSelf.toCpiListWindowView removeFromSuperview];
            [Global_Nav pushVCName:@"WXPToCPIListVC" animated:true];
        };
    }
    return  _toCpiListWindowView;
}
// clound 信用报告 - 彩虹
- (WXPHomeCloundReportView *)cloundReportView
{
    if (_cloundReportView == nil) {
        _cloundReportView = [WXPHomeCloundReportView  new];
    }
    return  _cloundReportView;
}
// FICO 信用报告
- (WXPFicoReport *)ficoReport
{
    if (_ficoReport == nil) {
        _ficoReport = [WXPFicoReport  new];
    }
    return  _ficoReport;
}
// 修改银行卡
- (WXPAlreadyAuthAmendBankCardView *)amendBankCardView
{
    if (_amendBankCardView == nil) {
        _amendBankCardView = [WXPAlreadyAuthAmendBankCardView  new];
    }
    return  _amendBankCardView;
}
- (WXPAlreadyAuthGoPayBackView *)goPayBackView{
    if (!_goPayBackView) {
        _goPayBackView = [WXPAlreadyAuthGoPayBackView new];
    }
    return _goPayBackView;
}
- (WXPBannerView *)bannerView{
    if (_bannerView == nil) {
        _bannerView = [WXPBannerView  new];
    }
    return  _bannerView;
}




#pragma mark - viewDidLoad
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    // request
    [self requestUserCreditInfo];
    [self requestCheckUserLoanStatus];
    [self requestRepaidOrder];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableHeaderTopBg.backgroundColor = [UIColor clearColor];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.viewBG.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = [UIColor clearColor];
    
    [self resetHeaderView];
}

#pragma mark - resetHeaderView
- (void)resetHeaderView{
    self.headerView = [UIView initWithViews:@[
        self.navView,
        [GlobalMethod getViewWithHeight:LEFT_MARDIN],
        self.limitView,
        self.goLoanView,
        self.isHideCloundun == true ? [UIView new] : self.cloundReportView,
        self.isHideFICO == true ? [UIView new] : self.ficoReport,
        self.isShowGoPayBackView ? self.goPayBackView : [UIView new],
        self.isShowAmendBankCardView ? self.amendBankCardView : [UIView new],
        isAry(self.bannerView.bannerList) ? self.bannerView : [UIView new],
    ]];
    self.tableView.tableHeaderView = self.headerView;
}

#pragma mark - request
// 获取首页需要的用户信用额度
- (void)requestUserCreditInfo{
    [RequestApi requestUserCreditInfoWithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        ModelUserCreditInfo * model = [GlobalMethod exchangeDicToModel:response modelName:@"ModelUserCreditInfo"];
        [self.limitView resetWithModel:model];
        [self.goLoanView resetViewWithMoney:strDot2F(model.unused)];
        [self resetHeaderView];
        
        
//        [self.pedirPrestamoView resetViewWithMoney:[GlobalMethod formattedDouble:model.unused]];
//        // 弹窗
//        UIWindow * window = [UIApplication sharedApplication].windows.firstObject;
//        [window endEditing:true];
//        if(model.extraAmountIsNotReminded){
//            if(self.afterRepaymentHomePagePopView.haveShown == false){
//                [window addSubview:self.afterRepaymentHomePagePopView];
//                [self.afterRepaymentHomePagePopView startTheCountdown];
//                self.afterRepaymentHomePagePopView.haveShown = true;
//            }
//        }
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}
// 是否跳转CPI
- (void)requestIsJumpToCPI{
    [RequestApi requestIsJumpToCpiWithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        NSString * dataName = (NSString *)response;
        UIWindow * window = [UIApplication sharedApplication].windows.firstObject;
        [window endEditing:true];
        
        if([dataName isEqualToString:@"no_product_no_cpi"]){
            // 展示无产品弹窗，弹窗内按钮点击，取消弹窗显示
            [window addSubview:self.noProductWindowView];
        }else if([dataName isEqualToString:@"to_cpi"]){
            [window addSubview:self.toCpiListWindowView];
        }else if([dataName isEqualToString:@"to_product"]){
            // 跳转到首页-认证完成-产品打包页
            [Global_Nav pushVCName:@"WXPProductPackageVC" animated:true];
        }
        if (self.blockRequestIsJumpToCPIFinish) {
            self.blockRequestIsJumpToCPIFinish();
        }
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        if (self.blockRequestIsJumpToCPIFinish) {
            self.blockRequestIsJumpToCPIFinish();
        }
    }];
}

// 查询用户放款状态接口
- (void)requestCheckUserLoanStatus{
    [RequestApi requestCheckUserLoanStatusWithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        // 若用户放款失败，即【loanFail】字段为true，展示该区域(修改银行卡)，否则隐藏
        bool result = [response boolValueForKey:KEY_loanFail];
        if (self.isShowAmendBankCardView != result) {
            self.isShowAmendBankCardView = result;
            [self resetHeaderView];
        }
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}
// 待还款借款列表
- (void)requestRepaidOrder{
    [RequestApi requestRepaidOrder2WithDelegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        NSArray * repaidOrders = [response arrayValueForKey:KEY_repaidOrders];
        self.isShowGoPayBackView = isAry(repaidOrders);
        [self.goPayBackView resetViewWithList:repaidOrders];
        
        [self resetHeaderView];
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}


@end
